<template>
  <div class="artist-management">
    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索画家ID/姓名/风格"
        style="width: 360px"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增画家
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table 
      :data="filteredArtists" 
      border 
      style="width: 100%"
      header-row-class-name="table-header"
      stripe
      highlight-current-row
    >
      <el-table-column prop="artistId" label="画家ID" width="160" sortable align="center" />
      <el-table-column prop="artistName" label="画家姓名" width="180" />
      <el-table-column prop="nationality" label="国籍" width="120">
        <template #default="{ row }">
          <el-tag effect="plain" type="info">
            {{ row.nationality }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="artStyle" label="艺术风格" width="140">
        <template #default="{ row }">
          <el-tag effect="plain" :type="styleTagType[row.artStyle]">
            {{ row.artStyle }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="activeStatus" label="活跃状态" width="140">
        <template #default="{ row }">
          <el-tag :type="statusTagType[row.activeStatus]" effect="light">
            {{ statusOptions.find(s => s.value === row.activeStatus)?.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="certificate" label="代表证书" width="200" />
      <el-table-column prop="joinDate" label="加入日期" width="140" sortable />
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" plain @click="handleEdit(row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            plain
            @click="handleDelete(row.artistId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditMode ? '编辑画家信息' : '新增画家'"
      width="680"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="画家ID" prop="artistId">
          <el-input 
            v-model="formData.artistId" 
            :disabled="isEditMode"
            placeholder="ART开头+7位数字"
          />
        </el-form-item>
        <el-form-item label="画家姓名" prop="artistName">
          <el-input v-model="formData.artistName" placeholder="请输入画家全名"/>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="formData.nationality" placeholder="国家/地区"/>
        </el-form-item>
        <el-form-item label="艺术风格" prop="artStyle">
          <el-select v-model="formData.artStyle" placeholder="请选择风格">
            <el-option
              v-for="item in styles"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="活跃状态" prop="activeStatus">
          <el-select v-model="formData.activeStatus" placeholder="请选择状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="代表证书" prop="certificate">
          <el-input v-model="formData.certificate" />
        </el-form-item>
        <el-form-item label="加入日期" prop="joinDate">
          <el-date-picker
            v-model="formData.joinDate"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 艺术风格配置
const styles = [
  '印象派',
  '古典主义',
  '抽象表现主义',
  '超现实主义',
  '立体主义',
  '现实主义',
  '后现代主义'
]

// 活跃状态选项
const statusOptions = [
  { value: '活跃', label: '创作活跃期' },
  { value: '半休', label: '半退休状态' },
  { value: '已故', label: '已故大师' },
  { value: '新锐', label: '新锐画家' }
]

// 标签样式映射
const styleTagType = {
  '印象派': 'primary',
  '古典主义': 'success',
  '抽象表现主义': 'warning',
  '超现实主义': 'danger',
  '立体主义': 'info',
  '现实主义': '',
  '后现代主义': ''
}

const statusTagType = {
  '活跃': 'success',
  '半休': 'warning',
  '已故': 'danger',
  '新锐': ''
}

// 初始画家数据（7条示例）
const artists = ref([
  {
    artistId: 'ART2024001',
    artistName: '文森特·梵高',
    nationality: '荷兰',
    artStyle: '后印象派',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2099 5151',
    joinDate: '2015-03-15'
  },
  {
    artistId: 'ART2024002',
    artistName: '巴勃罗·毕加索',
    nationality: '西班牙',
    artStyle: '立体主义',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2283 6283',
    joinDate: '2016-06-20'
  },
  {
    artistId: 'ART2024003',
    artistName: '克劳德·莫奈',
    nationality: '法国',
    artStyle: '印象派',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2134 5678',
    joinDate: '2018-09-10'
  },
  {
    artistId: 'ART2024004',
    artistName: '萨尔瓦多·达利',
    nationality: '西班牙',
    artStyle: '超现实主义',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2282 9119',
    joinDate: '2020-08-01'
  },
  {
    artistId: 'ART2024005',
    artistName: '杰克逊·波洛克',
    nationality: '美国',
    artStyle: '抽象表现主义',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2280 1847',
    joinDate: '2019-04-12'
  },
  {
    artistId: 'ART2024006',
    artistName: '安迪·沃霍尔',
    nationality: '美国',
    artStyle: '波普艺术',
    activeStatus: '已故',
    certificate: 'ISNI 0000 0001 2281 0285',
    joinDate: '2021-11-05'
  },
  {
    artistId: 'ART2024007',
    artistName: '草间弥生',
    nationality: '日本',
    artStyle: '当代艺术',
    activeStatus: '活跃',
    certificate: 'ISNI 0000 0001 2283 4567',
    joinDate: '2022-02-28'
  }
])

// 搜索功能
const searchText = ref('')
const filteredArtists = computed(() => {
  return artists.value.filter(artist => {
    return Object.values(artist).some(value => 
      String(value).toLowerCase().includes(searchText.value.toLowerCase())
  )})
})

// 对话框相关状态
const dialogVisible = ref(false)
const isEditMode = ref(false)
const formRef = ref(null)
const formData = reactive({
  artistId: '',
  artistName: '',
  nationality: '',
  artStyle: '',
  activeStatus: '活跃',
  certificate: '',
  joinDate: new Date().toISOString().split('T')[0]
})

// 表单验证规则
const formRules = {
  artistId: [
    { required: true, message: '请输入画家ID', trigger: 'blur' },
    { pattern: /^ART\d{7}$/, message: '格式要求：ART+7位数字', trigger: 'blur' }
  ],
  artistName: [{ required: true, message: '请输入画家姓名', trigger: 'blur' }],
  artStyle: { required: true, message: '请选择艺术风格', trigger: 'change' },
  certificate: [
    { required: true, message: '请输入代表证书', trigger: 'blur' },
    { pattern: /^ISNI\s\d{4}\s\d{4}\s\d{4}\s\d{4}$/, message: 'ISNI格式：ISNI 0000 0000 0000 0000', trigger: 'blur' }
  ],
  joinDate: [{ required: true, message: '请选择加入日期', trigger: 'change' }]
}

// 操作函数
const handleAdd = () => {
  isEditMode.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEditMode.value = true
  Object.assign(formData, { ...row })
  dialogVisible.value = true
}

const handleDelete = (artistId) => {
  ElMessageBox.confirm('确定要删除该画家吗？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    artists.value = artists.value.filter(u => u.artistId !== artistId)
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  await formRef.value.validate()
  if (isEditMode.value) {
    const index = artists.value.findIndex(u => u.artistId === formData.artistId)
    artists.value.splice(index, 1, { ...formData })
  } else {
    if (artists.value.some(u => u.artistId === formData.artistId)) {
      return ElMessage.warning('画家ID已存在')
    }
    artists.value.push({ ...formData })
  }
  dialogVisible.value = false
  ElMessage.success(isEditMode.value ? '修改成功' : '新增成功')
}

const resetForm = () => {
  formData.artistId = ''
  formData.artistName = ''
  formData.nationality = ''
  formData.artStyle = ''
  formData.activeStatus = '活跃'
  formData.certificate = ''
  formData.joinDate = new Date().toISOString().split('T')[0]
}
</script>

<style scoped lang="scss">
.artist-management {
  padding: 24px;
  background: #ffffff;
  min-height: 100vh;

  .operation-bar {
    padding: 16px 24px;
    background: #f8f9fa;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.05);
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    border: 1px solid #e9ecef;
  }

  :deep(.table-header) th {
    background-color: #f8f9fa !important;
    color: #2c3e50;
    font-weight: 600;
    height: 56px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0,0,0,0.05);
    background: #ffffff;
    border: 1px solid #e9ecef;
    
    .el-tag {
      font-size: 13px;
      padding: 0 8px;
      height: 26px;
      line-height: 24px;
    }
  }
}

.el-dialog {
  border-radius: 12px;
  background: #ffffff;
  
  .el-dialog__header {
    border-bottom: 1px solid #e9ecef;
    margin: 0 24px;
    padding: 24px 0 16px;
    color: #2c3e50;
  }
  
  .el-dialog__body {
    padding: 24px 32px;
    color: #495057;
  }
  
  .el-dialog__footer {
    border-top: 1px solid #e9ecef;
    padding: 16px 24px;
  }
}

.el-form-item {
  margin-bottom: 20px;
  :deep(.el-form-item__label) {
    color: #2c3e50;
  }
}
</style>